package grapho;

import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public final class Montagem extends DirectedSparseMultigraph<Vertice, Aresta>
{
    private Graph dataManager;
    private HashMap<Integer, Vertice> graphoVertice;
    private HashMap<String, Aresta> graphoAresta;
    
    public Montagem()
    {
        dataManager = new Graph();
        graphoVertice = new HashMap<Integer, Vertice>();
        graphoAresta = new HashMap<String, Aresta>();
        
        ArrayList<Aluno> alunos = dataManager.getTodoAluno();
        
        for(Aluno aluno : alunos)
        {
            graphoVertice.put(aluno.getRm(), new Vertice(aluno));
        }
        
        build();
    }
    
    public HashMap<Integer, Vertice> getGraphoVertice()
    {
        return graphoVertice;
    }
    
    public HashMap<String, Aresta> getArestas()
    {
        return graphoAresta;
    }
    
    private void build()
    {
        for(Map.Entry<Integer, Vertice> entry : graphoVertice.entrySet())
        {
            Vertice vertice = entry.getValue();

            addVertex(vertice);
            
            for(int friendRm : vertice.getAlunoRms())
            {
                Aresta aresta = new Aresta(vertice, graphoVertice.get(friendRm));
                
                addEdge(aresta, graphoVertice.get(vertice.getRm()), graphoVertice.get(friendRm));
                
                graphoAresta.put(generateEdgeKey(aresta.getAmigoOrigem().getRm(), aresta.getAmigoDestino().getRm()), aresta);
            }
        }
    }
    
    public String generateEdgeKey(int sourceRm, int targetRm)
    {
        return "" + sourceRm + "" + targetRm;
    }
}
